﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Data;

/// <summary>
/// Summary description for DALSanPham
/// </summary>
public class DALSanPham : StandardConnectDB, ISanPham
{
    IDanhMuc iDanhMuc;
	public DALSanPham()
	{
        iDanhMuc = new DALDanhMuc();

	}

    #region ISanPham Members

    public System.Data.DataTable SelectTopSanPham(int top)
    {
        Dictionary<String,Object> paramlist =new Dictionary<String,Object>();
       return this.DTExecuteSelect("selecttopsanpham",paramlist);

    }

    #endregion

    #region ISanPham Members


    public SanPham selectbyid(string id)
    {
        SanPham sp = new SanPham();       

        Dictionary<String, Object> paramlist = new Dictionary<String, Object>();
        paramlist.Add("@id", id);
        DataTable dt =this.DTExecuteSelect("selectspbyid", paramlist);
       // DataTable dt = this.DTExecuteSelect("selectspbyidghjgh", paramlist);
        if (dt != null)
        {
            sp = this.ConvertObjectDataTabletoSanPham(dt.Rows[0]);
        }
            
        return sp;
             
    }
    

    #endregion


    public DataTable getbymasp()
    {
        Dictionary<String, Object> paramlist = new Dictionary<String, Object>();
        return this.DTExecuteSelect("selectmasp", paramlist);
    }



    public bool insertsp(string masp,string tensp,float dongia,int soluong,string trangthai,string hinh,string madm)
    {
        Dictionary<String, Object> paramlist = new Dictionary<String, Object>();
        paramlist.Add("@masp",masp);
        paramlist.Add("@tensp",tensp);
        paramlist.Add("@dongia",dongia);
        paramlist.Add("@soluong",soluong);
        paramlist.Add("@trangthai",trangthai);
        paramlist.Add("@hinhsp",hinh);
        paramlist.Add("@madm",madm);
        return this.ExecuteUpdate("insetsp",paramlist);
    }

    #region ISanPham Members


    public DataTable selectSPbyDanhMuc(string MaDM)
    {
        Dictionary<string, object> paramlist = new Dictionary<string, object>();
        paramlist.Add("@MaDM", MaDM);

        DataTable dt = this.DTExecuteSelect("selectSPbyDanhMuc",paramlist);
        return dt;
    }

    public SanPham ConvertObjectDataTabletoSanPham(DataRow dr)
    {
        SanPham sp = new SanPham();

        sp.Masp = dr["MASP"].ToString();
        sp.Tensp = dr["TENSP"].ToString();
        sp.Dongia = int.Parse(dr["DONGIA"].ToString());
        sp.Soluong = int.Parse(dr["SOLUONG"].ToString());
        sp.Trangthai = dr["TRANGTHAI"].ToString();
        sp.Hinhsp = dr["HINHSP"].ToString();
        sp.DtoDanhMuc = iDanhMuc.getDanhmucByID(dr["MADM"].ToString());

        return sp;
    }

    #endregion

    #region ISanPham Members



    public DataTable SearchProduct(string categories, float costmin, float costmax, string productname)
    {
        Dictionary<String, Object> paramlist = new Dictionary<String, Object>();
        paramlist.Add("@catagories", categories);
        paramlist.Add("@costmin", costmin);
        paramlist.Add("@costmax", costmax);
        paramlist.Add("@nameProduct", productname);

        return this.DTExecuteSelect("spSearchProduct", paramlist);
    }


    #endregion
}